 //
//  WordViewController.m
//  MyProject
//
//  Created by dllo on 15/10/31.
//  Copyright (c) 2015年 蓝鸥0825. All rights reserved.
//


//      食话页面
#import "WordViewController.h"
#import "WHZAfnetWorking.h"

#import "WordHotTableViewModel.h"

#import "WordHotTabelHeaderView.h"
#import "WordHotTabelHeaderModel.h"
#import <MJRefresh.h>

#import "SearchViewController.h"
#import "NetWork.h"

@interface WordViewController ()
{
    NSInteger i;
}
@end

@implementation WordViewController


//      视图将要出现
- (void)viewWillAppear:(BOOL)animated{
    
    [NetWork netWork:self];
    
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title = @"食话";
    
    
    //      收索页面
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(clickSearch:)];
    
    
    //      tableView
    //      框架
    self.wordHotTableView = [[WordHotTableView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.wordHotTableView];
    self.wordHotTableView.VC = self;
    
    
    //      上拉加载
    [self createData];
    self.wordHotTableView.tableWordHot.header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        [self.wordHotTableView.tableWordHot.header beginRefreshing];
        [self createData];
    }];
    //      下拉刷新
    self.wordHotTableView.tableWordHot.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self createNewData];
    }];
    i = 1;
    
    

    
    
    
    //      tabelView 的 headerView
    //      框架
    WordHotTabelHeaderView *tableHeaderView = [[WordHotTabelHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 100 * 35)];
    self.wordHotTableView.tableWordHot.tableHeaderView = tableHeaderView;
    tableHeaderView.VC = self;
    
    //      解析
    [WHZAfnetWorking POSTJRAfnNetworkingWithUrl:@"http://api.meishi.cc/v5/meishiquan_index2.php?format=json" WithParameters:nil Block:^(id result) {
        
        //      给 headerView 的 collectionView 传值
        self.dictionaryWordHotHeaderCollection = result;
        self.arrayWordHotHeaderCollection = [NSMutableArray array];
        NSMutableArray *arrayHeader = [NSMutableArray array];
        arrayHeader = self.dictionaryWordHotHeaderCollection[@"column"];
        
        for (NSMutableDictionary *tempDic in arrayHeader) {
            
            WordHotTabelHeaderModel *modelWordHotTabelHeader = [[WordHotTabelHeaderModel alloc] init];
            [modelWordHotTabelHeader setValuesForKeysWithDictionary:tempDic];
            [self.arrayWordHotHeaderCollection addObject:modelWordHotTabelHeader];
        }
        
        tableHeaderView.arrayWordHotHeaderTabelCollection = self.arrayWordHotHeaderCollection;
        [tableHeaderView.wordHotHeaderTabelCollection reloadData];
        
        //      给 headerView 的 scollView传值
        self.arrayWordHotHeaderScollView = [NSMutableArray array];
        tableHeaderView.arrayWordHotHeaderTabelScrollView = self.dictionaryWordHotHeaderCollection[@"activities2"];
        [tableHeaderView reloadInputViews];
    }];
}



- (void)createData{
    
    //      数据解析
    [WHZAfnetWorking POSTJRAfnNetworkingWithUrl:@"http://api.meishi.cc/v5/meishiquan_index2.php?format=json" WithParameters:nil Block:^(id result) {
        
        self.arrayWordHotTabelSecond = [NSMutableArray array];
        
        self.dictionaryWordHotTabel = result;
        self.arrayWordHotTabel = self.dictionaryWordHotTabel[@"hot_topic"];//给第一行六边形赋值
        self.arrayJump = [NSMutableArray array];
        self.arrayJump = self.dictionaryWordHotTabel[@"hot_topic"];//给下面的 cell 赋值
        NSLog(@"%@", self.arrayJump);
        
        for (NSMutableDictionary *tempDic in self.arrayWordHotTabel) {
            WordHotTableViewModel *model = [[WordHotTableViewModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.arrayWordHotTabelSecond addObject:model];
        }
        self.wordHotTableView.arrayWordHotTabel = self.arrayWordHotTabelSecond;
//        self.wordHotTableView.arrayJump = self.arrayJump;
        [self.wordHotTableView.tableWordHot reloadData];
        [self.wordHotTableView.tableWordHot.header endRefreshing];
    }];
}



- (void)createNewData{
 
        i = i + 1;
        [WHZAfnetWorking GETJRAfnNetworkingWithUrl:[NSString stringWithFormat:@"http://api.meishi.cc/v5/hot_topic_more.php?format=json?source=android&page=%ld&format=json&", i]WithParameters:nil Block:^(id result) {
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            dic = result;
            NSArray *arr = [WordHotTableViewModel baseModelByArr:dic[@"hot_topic"] ];
            [self.arrayWordHotTabelSecond addObjectsFromArray:arr];
            self.wordHotTableView.arrayWordHotTabel = self.arrayWordHotTabelSecond;
            [self.wordHotTableView.tableWordHot reloadData];
            [self.wordHotTableView.tableWordHot.footer endRefreshing];
            
        }];
    
}





//      跳转到搜索页面
- (void)clickSearch:(UIButton *)button{
    
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
}


@end

